home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
7_10.lha
/
7_10
/
test1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
2KB
|
83 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
* based on cfront 2.0 b5 demo program alph.C */
include <debug.h> /* DELETE */
*ident "@(#)ctrans:demo/task/alph.C 1.1" */
**************************************************************************
Copyright (c) 1984 AT&T
All Rights Reserved
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
The copyright notice above does not evidence any
actual or intended publication of such source code.
****************************************************************************/
/ Demonstration of two processes communicating via semaphores.
include <process.h>
define K 10
lass Semaphore : public process_object
int sigs;
ublic:
Semaphore(int i =0) { sigs = i; }
int pending();
void wait();
void signal() { if (sigs++ == 0) alert(); }
;
oid Semaphore::wait()
for (;;)
{
if (--sigs >= 0)
return;
sigs++;
this_process()->pause(this);
}
nt Semaphore::pending()
return sigs <= 0;
emaphore* sema[2];
lass Test : public process
int svi;
ublic:
Test(int i) : (i ? "Alphonse" : "Gaston") { svi = i; }
long main();
;
ong Test::main()
for (register int n = K / 2; n--; )
{
sema[!svi]->wait();
sema[svi]->signal();
printf("%s: %d\n", svi ? "Alphonse" : "Gaston", n);
}
return 0; // "return" sets the exitcode
ain()
sema[0] = new Semaphore(1);
sema[1] = new Semaphore;
(new Test(0))->exec(); // create "Gaston" process; switch to it
(new Test(1))->exec(); // create "Alphonse" process; switch to it
printf("back in main\n");
main_process()->exit(0); // terminate main process
printf("should not get here\n");
return 0;